package us.gaaoc.framework.webapp.backingbeans;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.faces.model.SelectItem;

import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;

import us.gaaoc.framework.manager.RoleManager;
import us.gaaoc.framework.model.Court;
import us.gaaoc.framework.model.Organizations;
import us.gaaoc.framework.model.Person;
import us.gaaoc.framework.model.PersonOrganizations;
import us.gaaoc.framework.model.Roles;
import us.gaaoc.framework.webapp.backingbeans.filingSearch.SearchPage;
import us.gaaoc.framework.webapp.backingbeans.profile.Menu;

@SuppressWarnings(value = { "all" })
public class ChooseCourtBean extends BaseBackingBean {
	private String currentCourt;
	private HashMap<String, Court> courtIdList;
	private List<SelectItem> courtList;
	private Person person;
	private List<Organizations> personOrgs;
	
	
	public ChooseCourtBean() {
		courtIdList = new HashMap<String, Court>();
		courtList = new ArrayList<SelectItem>();

		String personUuid = (String) this.getFacesContext().getExternalContext().getSessionMap().get("personUuid");		
		person = this.getModelFacade().findPersonbyID(personUuid);

		this.getSessionMap().put("person",person);
		Hibernate.initialize(person.getOrganizations());
		Hibernate.initialize(person.getRoles());

		Iterator<PersonOrganizations> it = person.getOrganizations().iterator();

		while (it.hasNext()) {
			PersonOrganizations porg = it.next();
			// commented by Ramu (eppakayr)
			/*String id = porg.getCourtUuid();
			Court c = this.getModelFacade().findCourtByUuid(id);*/
			Court c = porg.getCourt();
			courtIdList.put(c.getName(), c);
			courtList.add(new SelectItem(c.getName()));
		}

		currentCourt = courtList.get(0).getLabel();

		personOrgs = new ArrayList<Organizations>();
		for (PersonOrganizations o : person.getOrganizations()) {
			Hibernate.initialize(o.getOrganization());
			personOrgs.add(o.getOrganization());
		}
	}

	public String selectCourt() {
		String successString = "";
		Menu menu = null;

		String courtUuid = courtIdList.get(currentCourt).getUuid();
		this.getFacesContext().getExternalContext().getSessionMap().put(
				"courtUuid", courtUuid);
		this.getFacesContext().getExternalContext().getSessionMap().put(
				"courtName", courtIdList.get(currentCourt).getName());

		List<Roles> personRoles = person.getRoles();

		RoleManager roleManager = new RoleManager(true);
		menu = roleManager.manageTabForUser(courtIdList.get(currentCourt).getName(),personRoles);

		for (Roles r : personRoles) {
			if (r.getRole().contains("admin")) {
				this.getFacesContext().getExternalContext().getSessionMap().put("role",r);
				successString = "adminDashboard";
				break;
			} else if (r.getRole().contains("clerk")) {
				this.getFacesContext().getExternalContext().getSessionMap().put("role",r);
				successString = "clerkDashboard";
			} else if (r.getRole().equals("public")) {
				this.getFacesContext().getExternalContext().getSessionMap().put("role",r);
				successString = "publicDashboard";
			} else if (r.getRole().equals("agent")) {
				this.getFacesContext().getExternalContext().getSessionMap().put("role",r);			
				successString = "agentDashboard";
			} else if (r.getRole().equals("attorney")) {
				this.getFacesContext().getExternalContext().getSessionMap().put("role",r);
				successString = "attorneyDashboard";
			} else if (r.getRole().contains("judge")) {
				this.getFacesContext().getExternalContext().getSessionMap().put("role",r);
				successString = "judgeDashboard";
				break;
			}
		}
		this.getFacesContext().getExternalContext().getSessionMap().put("menu",
				menu);
		this.getFacesContext().getExternalContext().getSessionMap().put(
				"person", person);
		this.getFacesContext().getExternalContext().getSessionMap().put(
				"court", courtIdList.get(currentCourt));
		this.getFacesContext().getExternalContext().getSessionMap().put(
				"personOrgs", personOrgs);
		
		//Fix for bug with search page not knowing the current court.
		this.getFacesContext().getExternalContext().getSessionMap().put(
				"searchPage",new SearchPage());
		
		return successString;
	}

	public String getCurrentCourt() {
		return currentCourt;
	}

	public void setCurrentCourt(String currentCourt) {
		this.currentCourt = currentCourt;
	}

	public List<SelectItem> getCourtList() {
		return courtList;
	}

	public void setCourtList(List<SelectItem> courtList) {
		this.courtList = courtList;
	}

}
